import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:
 * Date: 2025-04-27
 * Time: 13:59
 */
public class demo1 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //此处Callable只是定义了一个“带有返回值”的任务
        //并没有真的在执行，执行还是需要搭配Thread对象
        Callable<Integer> callable = new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                int result = 0;
                for (int i = 1; i <= 100; i++) {
                    result += i;
                }
                return result;
            }
        };

        FutureTask<Integer> futureTask = new FutureTask<>(callable);
        Thread t = new Thread(futureTask);
        t.start();

        //get 操作就是获取到 FutureTask 的返回值，这个返回值就来自于Callable的call方法
        //get 可能会阻塞，当前线程执行完毕，get拿到返回结果
        //如果当前线程还没执行完毕，get会一直阻塞
        System.out.println(futureTask.get());
    }
}
